
var  gulp         = require('gulp'),
     clean        = require('gulp-clean'),
     rev          = require('gulp-rev'),
     revCollector = require('gulp-rev-collector'),
     minifyCss    = require('gulp-minify-css'),
     minifyImg    = require('gulp-imagemin'),
     browserSync  = require('browser-sync'),
     reload       = browserSync.reload,
     cache        = require('gulp-cache'),
     uglify       = require('gulp-uglify'),
     minifyHtml   = require('gulp-html-minify'),
     argvs        = require('yargs').argv,
     p_name       = argvs.n,
     flags        = argvs.p || !argvs.d,
     sass         = require('gulp-sass'),
     dirname      = __dirname,
     plumber      = require('gulp-plumber'),
     cdn          = require('gulp-cdn-replace'),
     babel        = require('gulp-babel'),
     autoprefixer = require('gulp-autoprefixer');
     // es6transpiler = require('gulp-es6-transpiler');

/* src路径 */
var Src = {
  projectFolder:dirname+'/Projects/'+p_name,

  tempFold: dirname+'/.temp/'+p_name,
  tempFolder: dirname+'/Projects/'+p_name+'/zTemp',

  distFold: dirname+'/Dist/'+p_name,
  distFolder: dirname+'/Projects/'+p_name+'/zDist',

  versionFold: dirname+'/rev/'+p_name,
  versionFolder: dirname+'/Projects/'+p_name+'/zRev',

  css: dirname+'/Projects/'+p_name+'/css/*.{scss,css}',
  js: dirname+'/Projects/'+p_name+'/js/**/*.js',
  html: dirname+'/Projects/'+p_name+'/**/*.html',
  img:  dirname+'/Projects/'+p_name+'/{img,images}/**/**'
}
// ---------------------开发本地------------------------------------------------------------------------------------------
// clean 临时样式文件
gulp.task('cleanTempCss',function(){
  return gulp.src([Src.tempFold+'/css'])
    .pipe(clean());
})
gulp.task('cleanTempJs',function(){
  return gulp.src([Src.tempFold+'/js'])
    .pipe(clean());
})
// sass编译
gulp.task('sass',['cleanTempCss'],function(){
  return gulp.src(Src.css)
          .pipe(sass())
          .pipe(autoprefixer({
                      browsers: ['last 2 versions'],
                      cascade: false
                  }))
          .pipe(gulp.dest(Src.tempFold+"/css"))
          .pipe(browserSync.stream())
          .pipe(reload({
            stream: true
          }));
})
// es6编译成es5
gulp.task('es6',['cleanTempJs'],function() {
     return gulp.src(Src.js)
        .pipe(plumber())
        .pipe(babel({
            presets:['es2015']
        }))
        .pipe(gulp.dest(Src.tempFold+"/js"))
        .pipe(browserSync.stream())
        .pipe(reload({
            stream: true
          }));
        
})
// 起静态服务器
gulp.task('server',function(){
    if(!flags){
      console.log("in---开发环境---")
       gulp.start(['sass','es6']);
       // gulp.start('es6');
    }else{
      console.log("in---生产环境---打包");
      return gulp.start('default');
    }
    browserSync.init({
      notify: false,
      port:8000,
      server:{
        baseDir:[Src.tempFold,dirname+'/Projects/'+p_name],
        index:"index.html",
        routes:{
          "css":"css",
          "js":".temp/"+p_name+"/js",
        }
      }
    })
    // 监视文件改动——刷新
    gulp.watch(Src.css,["sass"]);
    gulp.watch(Src.js,["es6"]);
    gulp.watch([Src.js,Src.html,Src.img,Src.tempFold+'css/**/*.css',Src.tempFold+'js/**/*.js'] ).on('change',reload);
})
// -------------打包  dist -----------------------------------------------------------------------------------------------------
// clean 打包后的静态资源文件夹 和  版本号文件夹
gulp.task('clean',function(){
  return gulp.src([Src.distFold,Src.versionFold])
    .pipe(clean());
})
// Css-加版本号-压缩-进包
gulp.task('revCss',['clean'],function(){
   return gulp.src([Src.tempFold +'/**/**/*.css'])
    .pipe(rev())
    .pipe(minifyCss())
    .pipe(gulp.dest(Src.distFold))
    .pipe(rev.manifest())
    .pipe(gulp.dest(Src.versionFold+'/css'))
})
// Js-加版本号-压缩- 进包 
gulp.task('revJs',['clean'],function(){
   return gulp.src([Src.tempFold+'/js/**/*.js','!'+Src.tempFold+'/js/zepto.min.js','!'+Src.tempFold+'/js/jquery.min.js'])
    .pipe(rev())
    .pipe(uglify())
    .pipe(gulp.dest(Src.distFold+'/js'))
    .pipe(rev.manifest())
    .pipe(gulp.dest(Src.versionFold+'/js'))
})
gulp.task('moveNoVersionJs',['clean'],function(){
   return gulp.src([Src.projectFolder+'/js/jquery.min.js',Src.projectFolder+'/js/zepto.min.js'])
    .pipe(gulp.dest(Src.distFold+'/js'))
})

// Image-压缩-进包
gulp.task('dealImg',['clean'],function(){
   return gulp.src([Src.projectFolder+'/**/**/*.{jpg,png}'])
      .pipe(cache(minifyImg({
            progressive: true,
            interlaced: true,
            svgoPlugins: [{cleanupIDs: false}]
          })))
      .pipe(gulp.dest(Src.distFold))
})

// tpl模板-加版本号-进包
// gulp.task('revHtml',['clean'],function(){
//     return gulp.src([dirname+'/src/**/*.html'])
//         .pipe(rev())
//         .pipe(minifyHtml())
//         .pipe(gulp.dest(dirname+'/dists'))
//         .pipe(rev.manifest())
//         .pipe(gulp.dest('./rev/html'))
// })

gulp.task('replaceVersionRoad',['clean','revCss','revJs','moveNoVersionJs'],function(){
   return gulp.src([Src.versionFold+'/**/*.json',Src.projectFolder+'/**/**/*.html',Src.distFold+'/**/*.js'])
    .pipe(revCollector())
    .pipe(gulp.dest(Src.distFold))
})
gulp.task('cdnReplace',['replaceVersionRoad'],function(){
    gulp.src([Src.distFold+'/**/*.html',Src.distFold+'/**/**/*.css'])
        .pipe(cdn({
            dir: Src.distFold,
            root: {
                js: '//m.ppdaicdn.com/act/'+p_name,
                css: '//m.ppdaicdn.com/act/'+p_name,
                image: '//m.ppdaicdn.com/act/'+p_name,
                cssImg:'//m.ppdaicdn.com/act/'+p_name
            }
        }))
        .pipe(gulp.dest( Src.distFold));
})
 

// 执行打包
gulp.task('default',['clean','revCss','cdnReplace','dealImg'])
 
